package com.example.flower_shop.repository;

import com.example.flower_shop.entity.CustomerOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@Repository
public interface OrderRepository extends BaseRepository<CustomerOrder, Long> {

    Optional<CustomerOrder> findByOrderNo(String orderNo);

    Page<CustomerOrder> findByUserId(Long userId, Pageable pageable);

    List<CustomerOrder> findByUserIdAndStatus(Long userId, Integer status);

    boolean existsByOrderNo(String orderNo);

    List<CustomerOrder> findByUserId(Long userId);

    Page<CustomerOrder> findByUserIdOrderByCreateTimeDesc(Long userId, Pageable pageable);

    Page<CustomerOrder> findByStatusOrderByCreateTimeDesc(Integer status, Pageable pageable);

    @Query("SELECT COALESCE(SUM(o.totalAmount), 0) FROM CustomerOrder o WHERE o.status IN (2, 3)")
    BigDecimal calculateTotalIncome();
}